package cn.skywalks.common.utils.date;

import cn.skywalks.common.utils.StringUtil;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lpeng on 2017/7/17.
 */
public class DateTime {

    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    private int second;
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 构造函数
     *
     * @param date java.util.Data
     */
    public DateTime(Date date) {
        this.clean();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        this.setDatetime(calendar);
    }

    /**
     * 构造函数
     *
     * @param c java.util.Calendar
     */
    public DateTime(Calendar c) {
        this.clean();
        this.setDatetime(c);
    }

    /**
     * 构造函数
     *
     * @param s "2017-07-18 14:39:25"
     */
    public DateTime(String s){
        this.clean();
        SimpleDateFormat f = this.getSimpleDateFormat();
        Calendar c = Calendar.getInstance();
        Date d = null;
        try {
            d = f.parse(s);
            c.setTime(d);
            this.setDatetime(c);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取年
     *
     * @return year
     */
    public int getYear() {
        return this.year;
    }

    /**
     * 获取月
     *
     * @return month
     */
    public int getMonth() {
        return this.month;
    }

    /**
     * 获取日
     *
     * @return day
     */
    public int getDay() {
        return this.day;
    }

    /**
     * 获取小时
     *
     * @return hour
     */
    public int getHour() {
        return this.hour;
    }

    /**
     * 获取分钟
     *
     * @return minute
     */
    public int getMinute() {
        return this.minute;
    }

    /**
     * 获取秒
     *
     * @return second
     */
    public int getSecond(){
        return this.second;
    }

    /**
     * 获取日期
     *
     * @return "2017-07-18"
     */
    public String getDate(){
        StringBuilder builder = new StringBuilder();
        builder.append(this.convert(this.year));
        builder.append("-");
        builder.append(this.convert(this.month));
        builder.append("-");
        builder.append(this.convert(this.day));
        return builder.toString();
    }

    /**
     * 获取时间
     *
     * @return "2017-07-18 14:39:25"
     */
    public String getDateTime(){
        StringBuilder builder = new StringBuilder();
        builder.append(getDate() + " ");
        builder.append(this.convert(this.hour));
        builder.append(":");
        builder.append(this.convert(this.minute));
        builder.append(":");
        builder.append(this.convert(this.second));
        return builder.toString();
    }

    /**
     * 获取当天是一周中的第几天（周一为第一天）
     *
     * @return int
     */
    public int getDayOfWeek() {
        Calendar c = this.toCalendar();
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        return (--dayOfWeek) == 0 ? 7 : dayOfWeek;
    }

    /**
     * 将当前时间转化为java.util.Calendar
     *
     * @return java.util.Calendar
     */
    public Calendar toCalendar() {
        Calendar c = Calendar.getInstance();
        c.set(this.year, this.month - 1, this.day, this.hour, this.minute);
        return c;
    }

    private String convert(int i) {
        return i < 10 ? "0" + String.valueOf(i) : String.valueOf(i);
    }

    private void setDatetime(Calendar c) {
        this.year = c.get(Calendar.YEAR);
        this.month = c.get(Calendar.MONTH) + 1;
        this.day = c.get(Calendar.DAY_OF_MONTH);
        this.hour = c.get(Calendar.HOUR_OF_DAY);
        this.minute = c.get(Calendar.MINUTE);
        this.second = c.get(Calendar.SECOND);
    }

    private void clean() {
        this.year = 0;
        this.month = 0;
        this.day = 0;
        this.hour = 0;
        this.minute = 0;
        this.second = 0;
    }


    /**
     * 获取时间格式话工具
     *
     * @return java.text.SimpleDateFormat
     */
    public static SimpleDateFormat getSimpleDateFormat(){
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
        return format;
    }

    /**
     * 获取时间格式话工具
     *
     * @param s 格式化方式
     * @return java.text.SimpleDateFormat
     */
    public static SimpleDateFormat getSimpleDateFormat(String s){
        if (StringUtil.isBlank(s)){
            return getSimpleDateFormat();
        }
        return new SimpleDateFormat(s);
    }

    /**
     * 当前日期
     *
     * @return "2017-07-18"
     */
    public static String getCurrentDate2String() {
        String t = (new Timestamp(System.currentTimeMillis())).toString();
        return t.substring(0, 11);
    }

    /**
     * 当前时间
     *
     * @return "2017-07-18 14:39:25"
     */
    public static String getCurrentDateTime2String(){
        String t = (new Timestamp(System.currentTimeMillis())).toString();
        return t.substring(0, 19);
    }

}
